package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.basebean.BaseRespVo;
import com.cskaoyan.bean.wx_search.SearchIndexVo;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.KeyWordService;
import com.cskaoyan.service.SearchHistoryService;
import com.cskaoyan.utils.UserUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Set;

//chaged by zhao
@RestController
@RequestMapping("wx/search")
public class WxSearchController {
    @Autowired
    GoodsService goodsService;

    @Autowired
    KeyWordService keyWordService;

    @Autowired
    SearchHistoryService searchHistoryService;

    @RequestMapping("index")
    public BaseRespVo getIndexInfo() {
        User user = UserUtil.getUser();
        String username = null;
        if (user != null) {
            username = user.getUsername();
        }
        SearchIndexVo searchIndexVo = keyWordService.selectSearchIndexVo(username);

        return BaseRespVo.ok(searchIndexVo);
    }

    @RequestMapping("helper")
    public BaseRespVo helper(String keyword) throws IOException {
        keyword = URLDecoder.decode(keyword, "utf-8");
        Set<String> helpList = goodsService.helpSearch(keyword);
        return BaseRespVo.ok(helpList);
    }

    @RequestMapping("clearhistory")
    public BaseRespVo clearHistory() {
        User user = UserUtil.getUser();
        String username = null;
        if (user != null) {
            username = user.getUsername();
        }
        boolean result = searchHistoryService.deleteSearchHistory(username);
        return result?BaseRespVo.ok():BaseRespVo.fail();
    }
}
